package com.xckj.cycling.dialog

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.junca.android.service.Request
import com.xckj.cycling.R
import com.xckj.cycling.databinding.DialogRegionBinding
import com.xckj.cycling.model.LocationDetailModel
import com.xckj.cycling.model.LocationModel
import com.xckj.cycling.utils.AndroidUtil
import com.xckj.cycling.view.wheelview.OnWheelScrollListener
import com.xckj.cycling.view.wheelview.WheelView
import com.xckj.cycling.view.wheelview.adapter.AbstractWheelTextAdapter1
import kotlinx.coroutines.launch

class RegionDialog(private val context: FragmentActivity, val callback: (String?) -> Unit) :
    BaseDialog(context) {

    lateinit var viewBinding: DialogRegionBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val vb = DialogRegionBinding.inflate(layoutInflater, window?.decorView as ViewGroup?, false)
        viewBinding = vb
        setContentView(vb.root)
        vb.cancel.setOnClickListener {
            dismiss()
        }
        vb.confirm.setOnClickListener {
            val adapter1: Adapter = vb.wvBirthYear.viewAdapter as Adapter
            val adapter2: Adapter = vb.wvBirthMonth.viewAdapter as Adapter
            val text1 = adapter1.list.get(vb.wvBirthYear.currentItem).text
            val text2 = adapter2.list.get(vb.wvBirthMonth.currentItem).text
            if(AndroidUtil.isChinese(context)){
                callback.invoke(String.format("%s,%s", text1, text2))
            }else{
                callback.invoke(String.format("%s,%s", text2, text1))
            }
            dismiss()
        }
        context.lifecycleScope.launch {
            val r = Request.postSuspend<LocationModel>("qixing/user/locationinfo", HashMap())
            if (r.isSuccessful) {
                val d = r.data?.Location?.regioncolumns?.get(0) ?: return@launch
                vb.wvBirthYear.viewAdapter = Adapter(context, d)
                vb.wvBirthYear.visibleItems = 10
                vb.wvBirthMonth.visibleItems = 10
                vb.wvBirthYear.addScrollingListener(object : OnWheelScrollListener {
                    override fun onScrollingStarted(wheel: WheelView?) {
                    }

                    override fun onScrollingFinished(wheel: WheelView) {
                        val index = wheel.currentItem
                        findChild(d, index)
                        setTextViewSize(
                            d.get(wheel.currentItem).text ?: "",
                            wheel.viewAdapter as AbstractWheelTextAdapter1
                        )
                    }

                })
                vb.wvBirthYear.scroll(0,0)
            }
        }
    }

    private fun findChild(d: List<LocationDetailModel>, index: Int) {
        val r = d.get(index).children ?: emptyList()
        viewBinding.wvBirthMonth.viewAdapter = Adapter(context, r)
        viewBinding.wvBirthMonth.clearListener()
        viewBinding.wvBirthMonth.addScrollingListener(object : OnWheelScrollListener {
            override fun onScrollingStarted(wheel: WheelView?) {
            }

            override fun onScrollingFinished(wheel: WheelView) {
                setTextViewSize(
                    r.get(wheel.currentItem).text ?: "",
                    wheel.viewAdapter as AbstractWheelTextAdapter1
                )
            }

        })
        viewBinding.wvBirthMonth.scroll(0,0)
    }

    /**
     * 设置字体大小
     *
     * @param adapter
     */
    private fun setTextViewSize(
        text: String,
        adapter: AbstractWheelTextAdapter1
    ) {
        val arrayList: ArrayList<View> = adapter.getTestViews()
        val size = arrayList.size
        for (i in 0 until size) {
            val textvew = arrayList[i] as TextView
            val t = textvew.text.toString()
            if (text == t) {
                textvew.textSize = 24f
                textvew.setTextColor(Color.parseColor("#0AD39F"))
            } else {
                textvew.textSize = 22f
                textvew.setTextColor(Color.parseColor("#808080"))
            }
        }
    }


    class Adapter(context: Context, val list: List<LocationDetailModel>) :
        AbstractWheelTextAdapter1(context, R.layout.item_birth_year, R.id.tempValue, 0, 24, 22) {
        override fun getItemsCount(): Int {
            return list.size
        }

        override fun getItemText(index: Int): CharSequence {
            return list[index].text ?: ""
        }

    }
}